package org.holynite.fieldmanage.managed.backing

import org.holynite.fieldmanage.ejbs.AuthorizationManagement
import org.holynite.fieldmanage.managed.model.RoleManageFormBean
import org.holynite.fieldmanage.managed.support.RolesBean

import javax.ejb.EJB
import javax.faces.bean.ManagedBean
import javax.faces.bean.ManagedProperty
import javax.faces.bean.RequestScoped
import javax.faces.event.ValueChangeEvent

/**
 * Created by midnite on 15/1/31.
 */
@ManagedBean
@RequestScoped
class RoleManageSelectBean implements Serializable {

    @EJB(beanName = "adminRoleControl")
    AuthorizationManagement roleService
    @ManagedProperty("#{roleManageFormBean}")
    RoleManageFormBean rmform
    @ManagedProperty("#{rolesBean}")
    RolesBean rolesBean

    void listRoleDetail(ValueChangeEvent event) {
        def selected = event.newValue as String
        if (selected.equals("new_content")) {
            rmform.addMode = true
            rmform.clearForm()
        } else {
            rmform.addMode = false
            def role = rolesBean.roles.find { it.svrid.equals(selected) }
            if (!role.privileges || role.privileges?.size() < 1) role = roleService.fillPrivilege(role)
            rmform.roleName = role.role
            rmform.selectedPrivileges = role.privileges.collect { it.privilege.codeSign }
        }
    }
}
